<?php 
namespace Admin\Controller;
//use Think\Controller;
use Tools\AdminController;
class RoleController extends AdminController {
//   展示页
    public function showlist(){
        $info = D('Role')->select();
        $this->assign('info',$info);
        $this->display();
    }  
//   
    public function distribute(){
        $role = new \Model\RoleModel();
        if(IS_POST){
//            dump($_POST);
            $ok = $role->saveAuth($_POST['role_id'],$_POST['auth_id']);
            if($ok){
               $this->redirect('showlist',array(),2,'分配权限成功');
               }else{
               $this->redirect('distribute',array('role_id'=>$_GET['role_id']),2,'分配权限失败');    
                }
        }else{
        $roles = $role->find($_GET['role_id']);
        $have_authids = $roles['role_auth_ids'];//101,102,102
        $have_authids = explode(',',$have_authids);
//        print_r($have_authids);
        $auth_infoA = D('Auth')->where('auth_level=0')->select();
        $auth_infoB = D('Auth')->where('auth_level=1')->select();
        $this->assign('infoA',$auth_infoA);
        $this->assign('infoB',$auth_infoB);
        $this->assign('role',$roles);
        $this->assign('have_ids',$have_authids);
        $this->display();
        }
    }
//添加角色
    public function addrole(){
        if(!IS_POST){
             $this->display();
        }else{
            $role = new \Model\RoleModel;
            $role_name = ['role_name'=>I('post.role_name'),'add_time'=>time()];
            $roleok = $role->add($role_name);
            if($roleok){
                $this->redirect('showlist',array(),2,'添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }
}


